<template>
    <div>
        <el-page-header @back='$router.back()'></el-page-header>
        <o-tips title='修改密码' desc='修改密码后，必须重新登录，如果忘记原密码，请联系管理员进行密码重置。'>
            <el-form label-width="120px" :model="formData" :rules='rules' ref='form'>
                <el-form-item label='原密码' prop='origin_password'>
                    <el-input v-model='formData.origin_password'></el-input>
                </el-form-item>
                <el-form-item label='新密码' prop='password'>
                    <el-input v-model='formData.password'></el-input>
                </el-form-item>
                <el-form-item label='确认新密码' prop='confirm_password'>
                    <el-input v-model='formData.confirm_password'></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button @click='$router.back()'>取消</el-button>
                    <el-button type='primary' @click='confirm'>确认修改</el-button>
                </el-form-item>
            </el-form>
        </o-tips>
    </div>
</template>

<script>
    import {MISS_REQUIRE_FORM_MESSAGE, TOKEN_KEY} from '@const';
    import {toast, formCheck, alert} from '@u'
    export default {
        data () {
            return {
                formData : {
                    origin_password : null,
                    password : '',
                    confirm_password : ''
                },
                rules : {
                    origin_password : [{required: true, message : MISS_REQUIRE_FORM_MESSAGE}],
                    password : [{required: true, message : MISS_REQUIRE_FORM_MESSAGE}],
                    confirm_password : [{required: true, message : MISS_REQUIRE_FORM_MESSAGE}],
                }
            }
        },
        created () {
            
        },
        methods : {
            confirm () {
                
                formCheck(this.$refs.form).then(_ => {
                    if (this.formData.password.length < 6 ) {
                        toast('请至少设置6位数密码')
                        return;
                    }
                    //验证两次密码是否一致
                    if (this.formData.password != this.formData.confirm_password) {
                        toast('两次输入的密码不一致')
                        return;
                    }

                    this.__api.system.changePwd(this.formData).then(_ => {
                        return alert('密码修改成功，请重新登录');
                    }).then(_ => {
                        localStorage.removeItem(TOKEN_KEY);
                        this.$router.replace('/login');
                    })
                })
            }
        }
    }
</script>

<style>

</style>